package com.lsnu.ehr.controller;

import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.lsnu.ehr.domain.Result;
import com.lsnu.ehr.entity.Salary;
import com.lsnu.ehr.jpa.SalaryRepository;
import com.lsnu.ehr.jpa.SalarySearchRepository;
import com.lsnu.ehr.jpa.SalarySortRepository;

/**
 * 薪酬控制器
 *
 */
//允许跨域请求
@CrossOrigin
// 以json形式返回
@ResponseBody
// 控制器
@Controller

public class SalaryController {
	
	// 依赖注入，类似于配置spring的bean
	@Autowired
	SalaryRepository repositories;
	@Autowired
	SalarySearchRepository salarySearchRepositories;
	@Autowired
	SalarySortRepository salarySortRepositories;
	
	@RequestMapping("addSalary")
	public Object addSalary(Salary salary) {
		
		// 使用接口直接存储部门，spring会自动代理接口方法時
		// 返回值是一个持久态的Depa
		Salary dbSalary = repositories.save(salary);
		
		boolean flag = (null != dbSalary.getId());
		
		return new Result(1,flag);
	}
	
	    // 允许跨域请求
		@CrossOrigin
		@RequestMapping("updateSalary")
		public Object updateSalary(Salary salary) {
			
			
			Salary dbSalary = repositories.save(salary);
			
			boolean flag = (null != dbSalary.getId());
			
			return new Result(1,flag);
		}
		
		// 允许跨域请求
		@CrossOrigin
		@RequestMapping("getSalaryById")
		public Object getSalaryById(@RequestParam Integer id) {
			
			// 使用接口直接存储部门，spring会自动代理接口方法時
			Optional<Salary> salaries = repositories.findById(id.longValue());
			
			return new Result(1, salaries);
		}
		
		@CrossOrigin
		@RequestMapping("getAllSalaries")
		public Object getAllSalaries() {
			
			// 使用接口直接存储部门，spring会自动代理接口方法時
			List<Salary> salaries = repositories.findAll();
			
			return new Result(1, salaries);
		}
		
		@CrossOrigin
		@RequestMapping("delSalary")
		public Object delSalary(@RequestParam Integer id)
		{
			System.out.println(id);
			repositories.deleteById(id.longValue());
			return new Result(1, id, "success");
		}
		
		@CrossOrigin
		@GetMapping("/findByNameLike")
	    public Object findByNameLike(String name) {
	        // 一定要加 "%"+参数名+"%"
			List<Salary> salaries = salarySearchRepositories.findBysalaryNameLike("%"+name+"%");
			return new Result(1, salaries, "success");
	        //return salarySearchRepositories.findBysalaryNameLike("%"+name+"%");
	    }
		
		@SuppressWarnings("deprecation")
		@CrossOrigin
		@RequestMapping("SortById")
		public Object SortAndPaging()
		{
			Order idOrder = new Order(Direction.DESC, "id");
			//Order nameOrder = new Order(Direction.DESC,"salary_name");
			
			Sort sort = new Sort(idOrder);
			List<Salary> salaries = (List<Salary>) this.salarySortRepositories.findAll(sort);
			for (Salary salary : salaries) {
				System.out.println(salary);
			}
			return new Result(1, salaries, "success");
		}
		
		@SuppressWarnings("deprecation")
		public void test_findAll_page(){
			int currentPage =0; //当前页从0 开始
			int pageSize = 5;
			
			//排序
			Order idOrder = new Order(Direction.DESC, "id");
			Order nameOrder = new Order(Direction.ASC,"name");
			Sort sort = new Sort(idOrder,nameOrder);
			PageRequest pageRequest  = new PageRequest(currentPage, pageSize, sort);
			
			Page<Salary> page = this.salarySortRepositories.findAll(pageRequest);
			System.out.println("总记录数:" + page.getTotalElements());
			System.out.println("总页数:" + page.getTotalPages());
			System.out.println("当前页（request):" + page.getNumber());
			System.out.println("当前页总记录数（request):" + page.getSize());
			System.out.println("当前页记录总数：" + page.getNumberOfElements());
			List<Salary> students = page.getContent();
			for (Salary studentPO : students) {
				System.out.println(studentPO);
			}
		}
		
}